#if UNITY_EDITOR
using System;

using System.Collections.Generic;
using System.IO;
using System.Text.RegularExpressions;
using UnityEngine;
using Newtonsoft.Json;
using UnityEngine.UI;

public  class LocalizationHelper
{
    
    [UnityEditor.MenuItem("Tools/Zhan/Switch to CN")]
    public static void SetLanguag2CN()
    {
        Localization.language = LanguageType.CN;
        GameDebug.Log("Switch to CN");
    }
    [UnityEditor.MenuItem("Tools/Zhan/Switch to EN")]
    public static void SetLanguag2EN()
    {
        Localization.language = LanguageType.EN;
        GameDebug.Log("Switch to EN");
    } 
    [UnityEditor.MenuItem("Tools/Zhan/Switch to TC")]
    public static void SetLanguag2TC()
    {
        Localization.language = LanguageType.EN;
        GameDebug.Log("Switch to TC");
    }


    //[UnityEditor.MenuItem("Tools/语言包/查找所有常量数据中使用的中文")]
    //public static void ShowAllJsonChinese()
    //{
    //    string textDir = Application.dataPath + "/DefaultTables";
        
    //    GameDebug.Log($"scripts:{textDir}");
    //    List<string> res = new List<string>();

    //    List<TextFile> allScripts = new  List<TextFile> ();

    //    GetAllScripts(textDir, allScripts);

    //    foreach (TextFile script in allScripts)
    //    {
    //        foreach (var line in script.lines)
    //        {
    //            //GameDebug.Log(line);
    //            if (ContainsChinese(line))
    //            {    
    //                if (line.Contains("\""))
    //                {
    //                    string[] contents = GetAllStringInDoubleQuotationMark(line);

    //                    foreach (var str in contents)
    //                    {
    //                        if (string.IsNullOrEmpty(str.Trim()))
    //                            continue;
                            
    //                        if (!ContainsChinese(str))
    //                        {
    //                            continue;
    //                        }
    //                        if(!res.Contains(str))
    //                            res.Add(str);
    //                    }
    //                }
    //            }
    //        }
    //    }
    //    string resStr = LinesToStrng(res.ToArray());

    //    File.WriteAllText(dataChineseFilePath,resStr);
    //}
    
    //[UnityEditor.MenuItem("Tools/语言包/查找所有代码使用的中文")]
    //public static void ShowAllChinese()
    //{
    //    string codeDir = Application.dataPath + "/0A2_Scripts/Scene";
        
    //    GameDebug.Log($"scripts:{codeDir}");
        
    //    List<string> res = new List<string>();

    //    List<TextFile> allScripts = new  List<TextFile> ();

    //    GetAllScripts(codeDir, allScripts,"_Auto","test","Debug");

    //    foreach (TextFile script in allScripts)
    //    {
    //        foreach (var line in script.lines)
    //        {
    //            if(line.Contains("UnityEditor"))
    //                break;
              
    //                //GameDebug.Log(line);
    //            if (ContainsChinese(line))
    //            {    
    //                if (line.Contains("\"") 
    //                    && !line.Contains("Debug.Log")
    //                    && !line.Contains("throw ")
    //                    && !line.Contains("<param name")
    //                    && !line.Contains("ContextMenu"))
    //                {
                        
    //                    string[] contents = GetAllStringInDoubleQuotationMark(line);

    //                    foreach (var str in contents)
    //                    {
    //                        if (string.IsNullOrEmpty(str.Trim()))
    //                            continue;
                            
    //                        if (!ContainsChinese(str))
    //                        {
    //                            continue;
    //                        }
                            
    //                        if (str.Contains("/"))
    //                        {
    //                            GameDebug.Log($"疑似路径：\"{str}\",已跳过，请核实");
    //                            continue;
    //                        } 
    //                        if (str.Contains("//\""))
    //                        {
    //                            GameDebug.Log($"疑似注释：{str},已跳过，请核实");
    //                            continue;
    //                        }
                        
    //                        if(!res.Contains(str))
    //                            res.Add(str);
    //                    }
                         

    //                }
    //            }
    //        }
    //    }


    //    string resStr = LinesToStrng(res.ToArray());

    //    File.WriteAllText(chineseFilePath,resStr);
    //}

    
    public static string[] GetAllStringInDoubleQuotationMark(string line)
    {
        List<string> res = new List<string>();

        while (line.Contains("\""))
        {
            int firstIndex = line.IndexOf('"');
            int endIndex = 0;
            for (int i = firstIndex + 1; i < line.Length; i++)
            {
                if (line[i].Equals('"'))
                {
                    endIndex = i;
                    break;
                }
            }

            int length = endIndex - firstIndex - 1;
            if (length > 0)
            {
                string str = line.Substring(firstIndex + 1, length);

                res.Add(str);
            }

            int nextBeginIndex = endIndex + 1;
            int nextLength = line.Length - endIndex - 1;

            if (nextBeginIndex < line.Length && nextLength >= 0 && nextBeginIndex + nextLength <= line.Length)
                line = line.Substring(endIndex + 1, nextLength);
            else
                break;
        }

        return res.ToArray();
    }


    private static string chineseFilePath => Application.dataPath+"/0A4_Config/AllChinese.txt";
    private static string dataChineseFilePath => Application.dataPath+"/0A4_Config/DataAllChinese.txt";
    
    static string LinesToStrng(string[] lines)
    {
        string res = "";
        foreach (var line in lines)
        {
            res += line + "\n";
        }

        return res;
    }

    public static bool ContainsChinese(string str)
    {
        if (Regex.IsMatch(str, @"[\u4e00-\u9fa5]+"))   
        {   
            Console.WriteLine("字符串中有汉字");
            return true;
        }   
        else  
        {   
            Console.WriteLine("字符串中无汉字");
            return false;
        }   
    }


    public class TextFile
    {
        public string name;
        public string path;
        public string[] lines;

        public string content => LinesToStrng(lines);
    }
    
    static void GetAllScripts(string dir,List<TextFile> scripts,params string[] skips )
    {
        if (scripts == null)
            return;
        
        DirectoryInfo scriptDir = new DirectoryInfo(dir);

        scriptDir.GetDirectories().ForEach(e => { GetAllScripts(e.FullName, scripts,skips); });
        
        scriptDir.GetFiles().ForEach(e =>
        {
            if (Path.GetExtension(e.Name).Equals(".cs")|| Path.GetExtension(e.Name).Equals(".json"))
            {
                if (skips != null)
                {
                    foreach (string skip in skips)
                    {
                        if (e.Name.Contains(skip))
                        {
                            return;
                        }
                    }
                  
                }
                
                //GameDebug.Log($"add scripts:{e.Name}");
                string[] lines = File.ReadAllLines(e.FullName);
                scripts.Add(new TextFile(){name = e.Name,path = e.FullName,lines = lines});
            }
        });

    }
    private static string localizationDataDir => Application.dataPath + "/Resources/Data/Shared/Localization";
    public static string localizationDataPath(LanguageType type) => Path.Combine(localizationDataDir, type.ToString()+".json");
    

}

#endif